package com.example.NearByLocation;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import com.baidu.mapapi.model.LatLng;
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/11/27.
 */
public class SearchBtActivity extends Activity {
    private Button searchback_tofirst_bt;
    private Button search_button;
    private EditText search_editview;
    private ListView search_ListView;
    private List<JsonData> list;
    private TextView location_TextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.searchbt);
        //取按键
        searchback_tofirst_bt = (Button) findViewById(R.id.searchback_tofirst_bt);
        search_button = (Button) findViewById(R.id.search_button);
        search_editview = (EditText) findViewById(R.id.search_editview);

        search_ListView = (ListView) findViewById(R.id.search_ListView);
        location_TextView = (TextView) findViewById(R.id.location_TextView);
        searchback_tofirst_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (search_editview.getText().toString().equals("输入商户名搜索") || search_editview.getText().toString().equals("")) {
                    Toast.makeText(SearchBtActivity.this, "请输入关键字", Toast.LENGTH_SHORT).show();
                } else {
                    String key = search_editview.getText().toString();
                    LocationManager location = new LocationManager(SearchBtActivity.this, location_TextView);
                    LatLng currentLat = location.currentLat;
                    if (currentLat == null) {
                        Toast.makeText(SearchBtActivity.this, "亲，还没定位哟！", Toast.LENGTH_SHORT).show();
                    } else {
                        searchData(key, currentLat.longitude, currentLat.latitude);
                    }
                }
            }
        });
    }

    public void searchData(final String keyWord, final double longi, final double leni) {
        final ProgressDialog dialog = new ProgressDialog(SearchBtActivity.this);
        dialog.setMessage("加载中...");
        dialog.show();
        AsyncTask<String, String, Integer> task = new AsyncTask<String, String, Integer>() {
            @Override
            protected Integer doInBackground(String... params) {
                HttpClientManager http = new HttpClientManager();
                try {
                    list = new ArrayList<JsonData>();
                    list = http.getData(keyWord, 3000, longi, leni,1);
                    Log.d("search", list.size() + "");
                    Log.d("search", longi + "");
                    Log.d("search", leni + "");
                    Log.d("search", keyWord);
                } catch (IOException e) {
                    return 1;
                } catch (JSONException e) {
                    return 2;
                }
                return 0;
            }

            @Override
            protected void onPostExecute(Integer integer) {
                if (integer == 1) {
                    Toast.makeText(SearchBtActivity.this, "网络通道阻塞", Toast.LENGTH_SHORT).show();
                } else if (integer == 2) {
                    Toast.makeText(SearchBtActivity.this, "服务器繁忙", Toast.LENGTH_SHORT).show();
                } else {
                    dialog.dismiss();
                    JsonDataAdapter dataAdapter = new JsonDataAdapter();
                    search_ListView.setAdapter(dataAdapter);
                    search_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            JsonData jsonData = list.get(position);
                            Intent intent = new Intent(SearchBtActivity.this, PoiListActivity.class);
                            SharedPreferences.Editor editor = getSharedPreferences("ServiceName", MODE_PRIVATE).edit();
                            editor.putString("thirdName", jsonData.getName());
                            editor.commit();
                            startActivity(intent);
                        }
                    });
                }
            }
        };
        task.execute();
    }

    class JsonDataAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            JsonData daa = (JsonData) getItem(position);
            View view;
            if (convertView == null) {
                view = getLayoutInflater().inflate(R.layout.poilist_item, parent, false);
            } else {
                view = convertView;
            }
            TextView poiName_text = (TextView) view.findViewById(R.id.poiName_text);
            TextView poiAddress_text = (TextView) view.findViewById(R.id.poiAddress_text);
            TextView poiDistance_text = (TextView) view.findViewById(R.id.poiDistance_text);
            poiName_text.setText(daa.getName());
            poiAddress_text.setText(daa.getAdd());
            if (Integer.parseInt(daa.getDistance()) <= 1000) {
                poiDistance_text.setText(daa.getDistance() + "m");
            } else if (Integer.parseInt(daa.getDistance()) > 1000) {
                double a = Integer.parseInt(daa.getDistance()) / 1000;
                poiDistance_text.setText(a + "km");
            }
            return view;
        }
    }
}
